package com.to.dao;

import com.to.bean.User;
import com.to.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDao {
    private QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
    public User getUserByNameAndPassword(String username,String pwd){
        String sql="select * from user where username = ? and password = ?";
        User user=null;
        try {
            user=queryRunner.query(sql,new BeanHandler<>(User.class),username,pwd);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;

    }

    public List<User> selectAllUser() {
        String sql = "select * from user";
        List<User> uli = null;
        try {
            uli = queryRunner.query(sql, new BeanListHandler<>(User.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return uli;
    }
}
